<template>
  <el-card>
    <el-checkbox-group v-model="checkedDept" @change="runFilter">
      <el-checkbox
        v-for="item in departmentList"
        :key="item.id"
        :label="item.name"
      />
    </el-checkbox-group>

    <el-checkbox-group v-model="checkedType" style="margin-top: 40px" @change="runFilter">
      <el-checkbox
        v-for="item in attendanceEnum.holidayType"
        :key="item.id"
        :label="item.id"
      >
        {{ item.value }}
      </el-checkbox>
    </el-checkbox-group>
  </el-card>
</template>

<script>
import { getDepartments } from '@/api/department'
import attendanceEnum from '@/api/constant/attendance'
export default {
  props: {
    dataList: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      departmentList: [],
      checkedDept: [],
      attendanceEnum,
      checkedType: []
    }
  },
  async created() {
    const { depts } = await getDepartments()
    this.departmentList = depts
  },
  methods: {
    runFilter() {
      const res = this.changeDept(this.dataList)
      const filterList = this.changeType(res)
      this.$emit('changeData', filterList)
    },
    changeDept(data) {
      const filterList = this.checkedDept.length === 0 ? data : data.filter(item => this.checkedDept.includes(item.departmentName))
      return filterList
    },
    changeType(data) {
      const filterList = data.map(user => {
        return {
          ...user,
          attendanceRecord: this.coverRecord(user.attendanceRecord)
        }
      })
      return filterList
    },
    coverRecord(dayArr) {
      return dayArr.map(day => {
        return {
          ...day,
          adtStatu: (this.checkedType.length === 0 || this.checkedType.includes(day.adtStatu.toString())) ? day.adtStatu : ''
        }
      })
    }
  }
}
</script>

<style>

</style>
